#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2024/2/3 下午11:43
# @Author : Shi Lei
# @File : PublicFunction.py

import os

container_dir = '/tmp/containerData' # Docker容器里的映射目录 /tmp/containerData
config_dir = 'Config'  # 开发时用

'''
container_path:宿主机挂载到容器中的目录（docker -v 宿主机目录:/tmp/containerData），开发程序时可设为空，从程序目录中取配置文件，正式部置时可以设为Docker映射目录中的配置文件的位置
'''

def get_basic_path():
    # if os.path.exists(path):   # 不能用这种方法判断，因为开发环境容器和应用环境容器都有可能映射相同的这个目录
    return container_dir

def get_config_dir():
    return config_dir

def get_current_directory():
    current_directory = os.getcwd()
    return current_directory

def find_files(directory, extension):
    """
    :param directory: 要查询目录
    :param extension: 要查询的文件的扩展名
    :return: 元组列表[(路径，文件),...,()]
    如：/tmp/containerData/m4s/1056732888 /tmp/containerData/m4s/1056732888/1056732888-1-30280.m4s
    """
    lst =[]
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.' + extension):
                file_path = os.path.join(root, file)
                tup = (root,file_path)
                # print(tup[0],tup[1])
                lst.append(tup)
    return lst

def test_find_files(directory, extension):
    rst =[]
    rst = find_files(directory, extension)
    # print(rst)
    if len(rst) >0:
        for tup in rst:
            print(tup[0], tup[1])
    else:
        print("查询的结果为空（列表）")


def is_valid_linux_path(path):
    """
    :param path:
    :return:
    """
    if not path or len(path) > 4096:  # Linux最大路径长度为4095字符
        return (False, "路径太长啦")
    try:
        os.stat(path)
        return (True, "")
    except OSError as e:
        # print("Error occurred while checking the validity of the path:", str(e))
        return (False, "路径错误:{0}".format(str(e)))

if __name__ == '__main__':
    print("Hello")
    test_find_files('/tmp/containerData/m4s', 'm4s')
    print("world")
